package org.lanqiao.taru.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.lanqiao.taru.annotation.Monitor;
import org.lanqiao.taru.common.Result;

import org.lanqiao.taru.domain.TbUserinfo;
import org.lanqiao.taru.service.UserinfoService;
import org.lanqiao.taru.utils.JwtUtil;
import org.lanqiao.taru.vo.AuthUserVo;
import org.lanqiao.taru.vo.UserinfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@Api(tags="用户管理")
@RestController
@RequestMapping("/userinfo")
public class UserinfoController {

    @Autowired
    UserinfoService userinfoService;


    @Autowired
    JwtUtil jwtUtil;

    /**
     * 查询所有的数据
     * @return
     */
    @Monitor
    @ApiOperation("获取用户列表")
    @GetMapping("/getUserinfosList")
    public Result  getUserinfosList111(){
        List<TbUserinfo>     list=  userinfoService.getUserinfosList();
        return Result.OK("成功",list);
    }


    /**
     * 查询分页数据
     * @return
     */
    @Monitor
    @ApiOperation("获取用户列表分页")
    @GetMapping("/getUserinfosPage")
    public Result  getUserinfosPage(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")  Integer  pageNum ,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")  Integer  pageSize
            ){

        PageHelper.startPage(pageNum,pageSize);
        List<TbUserinfo>     list=  userinfoService.getUserinfosPage();
        PageInfo<TbUserinfo> pageList = new PageInfo<>(list);
        return Result.OK("成功",pageList);
    }


    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @ApiOperation("根据编号获取用户信息")
    @GetMapping("/getUserinfoById/{id}")
    public Result  getUserinfoById( @PathVariable("id") Long  id){
        TbUserinfo    userinfo =  userinfoService.getUserinfoById(id);
        return Result.OK("成功",userinfo);
    }


    /**
     * 增加
     * @param tbUserinfo
     */
    @ApiOperation("保存用户信息")
    @PostMapping("/saveUserinfo")
    public  Result    saveUserinfo( @RequestBody TbUserinfo  tbUserinfo){
        userinfoService.saveUserinfo(tbUserinfo);
        return Result.OK("成功",null);
    }


    /**
     * 修改
     * @param tbUserinfo
     */
    @ApiOperation("更新用户信息")
    @PutMapping("/updateUserinfo")
    public Result   updateUserinfo(  @RequestBody TbUserinfo  tbUserinfo ){
        userinfoService.updateUserinfo(tbUserinfo);
        return Result.OK("成功",null);

    }


    /**
     * 删除
     * @param id
     */
    @ApiOperation("移除用户信息")
    @DeleteMapping("/removeUserinfo/{id}")
    public Result   removeUserinfo( @PathVariable("id")  Long  id){
        userinfoService.removeUserinfo(id);
        return Result.OK("成功",null);
    }


    /**
     * fetch("http://locolhost:3000/userinfo/login",{
     *      method:'post',
     *      body:JSON.stringify({
     *          username:"zhangsan",
     *          password:"123456"
     *
     *      })
     *
     * }
     *
     * )
     *
     * 登录
     *
     * @param authUser
     * @return
     */

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result   login( @RequestBody AuthUserVo authUser   ){
       TbUserinfo  tbUserinfo  = userinfoService.login2(authUser);
       if(tbUserinfo==null){
           return Result.FAIL("用户名或密码错误");
       }else{
           String  jwtToken =   jwtUtil.generateToken(authUser.getUsername());
           HashMap  map  =new HashMap();
           map.put("userinfo",tbUserinfo);
           map.put("token",jwtToken);
           return Result.OK("登录成功",map);
       }

    }


    /**
     * 使用Get方式传参
     *
     * fetch("http://locolhost:3000/userinfo/login?username=zhagnsan&password=123456")
     *
     *
     *
     * @param
     * @return
     */
    @GetMapping("/login2")
    public Result   login2(  @RequestParam(value = "username",required = true)  String username ,
                             @RequestParam(value = "password",required = true)  String password){
        TbUserinfo  tbUserinfo  = userinfoService.login3(username,password);
        if(tbUserinfo==null){
            return Result.FAIL("用户名或密码错误");
        }else{


            return Result.OK("登录成功",tbUserinfo);
        }

    }


    /**
     * 查询用户信息（携带文章）
     * @param id
     * @return
     */
    @GetMapping("/getUserinfoWithArticlesById/{id}")
    public Result  getUserinfoWithArticlesById( @PathVariable("id") Long  id){
        TbUserinfo    userinfo =  userinfoService.selectUserinfoWithArticlesById(id);
        return Result.OK("成功",userinfo);
    }


    @PostMapping("/getUserinfosByCondition")
    public Result  getUserinfosByCondition(  @RequestBody  UserinfoVo userinfoVo){
        PageHelper.startPage(userinfoVo.getPageNum(),userinfoVo.getPageSize());
        List<TbUserinfo>     list=  userinfoService.getUserinfosByCondition(userinfoVo);
        PageInfo<TbUserinfo> pageList = new PageInfo<>(list);
        return Result.OK("成功",pageList);
    }


            /**
             * 批量删除
             * @param list
             * @return
             */
            @PostMapping("/removeBatchById")
            public  Result  removeBatchById( @RequestBody  List<Long>   list){
                userinfoService.removeBatchById(list);
                return Result.OK("批量删除成功",list);
            }


    /**
     * 转账测试案例
     * @return
     */
    @GetMapping("/transfer")
        public  Result   transfer(){
            userinfoService.transfer(6,7,200);
           return  Result.OK("转账成功","");
        }


}
